package xyz.yuzegod.megawalls.classes.mythic.werewolf;

import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.*;
import xyz.yuzegod.megawalls.event.PlayerEnergyChangeEvent;
import xyz.yuzegod.megawalls.event.PlayerKillEvent;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.util.ItemBuilder;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Werewolf extends Classes
{
    public static final List<GamePlayer> skill;
    public static final List<GamePlayer> skillDamage;
    public static final Map<GamePlayer, Integer> skill2;
    
    public Werewolf() {
        super("Werewolf", "狼人", ChatColor.GREEN, Material.COOKED_BEEF, (byte)0, ClassesType.MYTHIC, new ClassesInfo.Orientation[] { ClassesInfo.Orientation.TANK, ClassesInfo.Orientation.WARRIOR }, ClassesInfo.Difficulty.TWO);
        this.setMainSkill(new MainSkill(this));
        this.setSecondSkill(new SecondSkill(this));
        this.setThirdSkill(new ThirdSkill(this));
        this.setCollectSkill(new FourthSkill(this));
        this.setDefaultSkin("eyJ0aW1lc3RhbXAiOjE1NDkyMDIyNjExMTUsInByb2ZpbGVJZCI6ImUzYjQ0NWM4NDdmNTQ4ZmI4YzhmYTNmMWY3ZWZiYThlIiwicHJvZmlsZU5hbWUiOiJNaW5pRGlnZ2VyVGVzdCIsInNpZ25hdHVyZVJlcXVpcmVkIjp0cnVlLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMzUxNmQwNDVlNzNlYmM1N2RhNDY2YTJmN2I0Mjg4MTU5MWI1OTkwMDEwNmEyOGY4ZmZhMGNiZGFhOGE5MTQ5In19fQ==", "UpX2geR+fVfuG5MTLf5ttJQVUJebckJSxtJk76l3iwtV3ceIPGhFqF1RPC27WY9UmZJmFZde3v8E1bw66Tt7Au5Ol4X/D2YC+tl1gxkLSJUNaG12O03x/od5uohux1OPtgof21UIVgyewh6C5OAVegmRHFnx7obeWyuTboymbIkwIsffkVx0sbzgNaFQkcrSVUQYa4OTZ5Bp4zLpDeRGG9XjlCmurnaGOPHJT8N298TD1qAHEHYAuMZQIEze4RHzux01DH8b8lVA0HW5mN7OS4QjGAMGeUhOLYKptCo2bkem+u0Rc6RxC2h2oPA1yUCf95hG8QUAy/b9qn2Y4B+EnUBwRZjJrojtX7n/QSThGlCegeASyZdehcrUnE5/xAcGhwQh68ympbVQmY4YmxzZfvGe2kxOI8fHFMmrkGQLFXwu+6ceeE9y09gjbujK3VM4hIvWYaBBfFWIX4VIJsrTVT1oIGmpV/jjZbcKXWAOFkG+j87hgqZjBXAPMJZ6uLJS5D/BBR83JjU5a8w0eX1pnMBXwTZae7hd27IQVEoePXnr9Zw4HNsxlpvU5Bclv7aR5mX1OtDH7LI6ktWlYw1v6+B7+X8iAG76zcYHQUQXyyWseZAeKu6KgbCLTCdFKcPH5/ksIRmfENQq/9hItRA3UZCdSgAUTWtV3U9xQkTWT+c=");
        this.setEquipmentPackage(new EquipmentPackage(this) {
            @Override
            public List<ItemStack> getEquipments(final int level) {
                final List<ItemStack> items = new ArrayList<ItemStack>();
                items.add(new ItemBuilder(Material.COMPASS).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 指南针").build());
                items.add(new ItemBuilder(Material.DIAMOND_PICKAXE).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 镐").setUnbreakable(true).addEnchantment(Enchantment.DIG_SPEED, 3).addEnchantment(Enchantment.DURABILITY, 3).build());
                items.add(new ItemBuilder(Material.ENDER_CHEST).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 末影箱").build());
                switch (level) {
                    case 1: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_CHESTPLATE).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 胸甲").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 治疗药水 (6❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 1)).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 速度药水 II 和 跳跃 V (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 1).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 牛排").build());
                        break;
                    }
                    case 2: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_CHESTPLATE).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 胸甲").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 治疗药水 (6❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 1)).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 速度药水 II 和 跳跃 V (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 牛排").build());
                        break;
                    }
                    case 3: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_CHESTPLATE).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 胸甲").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 速度药水 II 和 跳跃 V (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 牛排").build());
                        break;
                    }
                    case 4: {
                        items.add(new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_CHESTPLATE).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 胸甲").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 3).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 速度药水 II 和 跳跃 V (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 4).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 牛排").build());
                        break;
                    }
                    case 5: {
                        items.add(new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.DIAMOND_CHESTPLATE).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 胸甲").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 3).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 3, (byte)2).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 速度药水 II 和 跳跃 V (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 5).setDisplayName(Werewolf.this.nameColor + Werewolf.this.getDisplayName() + " 牛排").build());
                        break;
    }
                }
                if (!xyz.yuzegod.megawalls.util.ItemUtils.containsBow(items)) {
                    items.add(new ItemBuilder(Material.BOW).setDisplayName(nameColor + getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                }
                return items;
            }
            
            @Override
            public List<String> getInfo(final int level) {
                final List<String> lore = new ArrayList<String>();
                switch (level) {
                    case 1: {
                        lore.add(" §8▪ §7铁 剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7铁 胸甲");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7治疗药水 6❤");
                        lore.add(" §8▪ §7速度药水 II 和 跳跃 V");
                        lore.add("    §8▪ 0:15");
                        lore.add(" §8▪ §7牛排");
                        break;
                    }
                    case 2: {
                        lore.add(" §8▪ §7铁 胸甲");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §a+ 保护 I");
                        lore.add(" §8▪ §7牛排 §8x1 ➜ §ax2");
                        break;
                    }
                    case 3: {
                        lore.add(" §8▪ §7铁 胸甲");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I ➜ §aII");
                        lore.add(" §8▪ §7治疗药水 6❤ ➜ §a8❤");
                        lore.add(" §8▪ §7速度药水 II 和 跳跃 V §8x1 ➜ §ax2");
                        lore.add("    §8▪ 0:15");
                        lore.add(" §8▪ §7牛排 §8x2 ➜ §ax3");
                        break;
                    }
                    case 4: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7铁 胸甲");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 II ➜ §aIII");
                        lore.add(" §8▪ §7牛排 §8x3 ➜ §ax4");
                        break;
                    }
                    case 5: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7胸甲");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7速度药水 II 和 跳跃 V §8x2 ➜ §ax3");
                        lore.add("    §8▪ 0:15");
                        lore.add(" §8▪ §7牛排 §8x4 ➜ §ax5");
                        break;
                    }
                }
                return lore;
            }
        });
    }
    
    @Override
    public List<String> getInfo() {
        final List<String> lore = new ArrayList<String>();
        lore.add("§7你想要有技能来保证你持续点击左键.");
        lore.add("§7你想要在1v2中取得机会.");
        lore.add("§7有时觉醒,对月长嗥.");
        return lore;
    }
    
    @Override
    public int unlockCost() {
        return 3;
    }
    
    @Override
    public int energyMelee() {
        return 8;
    }
    
    @Override
    public int energyBow() {
        return 6;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        if (this.secondSkill.getSkillTip(gamePlayer) == null) {
            return this.mainSkill.getSkillTip(gamePlayer);
        }
        return this.mainSkill.getSkillTip(gamePlayer) + " " + this.secondSkill.getSkillTip(gamePlayer);
    }
    
    @Override
    public void run() {
        GamePlayer.getOnlinePlayers().stream().filter(gamePlayer -> !gamePlayer.isSpectator() && ClassesManager.getSelected(gamePlayer).equals(this)).forEach(gamePlayer -> gamePlayer.sendActionBar(this.getSkillTip(gamePlayer)));
    }
    
    @EventHandler
    public void onPlayerKill(final PlayerKillEvent e) {
        final GamePlayer killer = e.getKiller();
        final Classes classes = ClassesManager.getSelected(killer);
        if (classes.equals(this) && MegaWalls.getRandom().nextInt(100) <= this.collectSkill.getAttribute(killer.getPlayerStats().getKitStats(classes).getSkill4Level()) * 100.0) {
            killer.sendMessage("§e你的食肉技能掉落了额外的牛排！");
            killer.getPlayer().getInventory().addItem(new ItemStack[] { new ItemBuilder(Material.COOKED_BEEF, e.isFinalKill() ? 4 : 1).build() });
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerItemConsume(final PlayerItemConsumeEvent e) {
        if (e.getItem().getType() == Material.COOKED_BEEF) {
            final GamePlayer gamePlayer = GamePlayer.get(e.getPlayer().getUniqueId());
            final Classes classes = ClassesManager.getSelected(gamePlayer);
            if (classes.equals(this)) {
                this.thirdSkill.use(gamePlayer, gamePlayer.getPlayerStats().getKitStats(classes));
                if (e.getPlayer().getFoodLevel() == 20) {
                    e.getPlayer().setFoodLevel(19);
                }
            }
        }
        else if (e.getItem().getType() == Material.POTION) {
            final GamePlayer gamePlayer = GamePlayer.get(e.getPlayer().getUniqueId());
            final Classes classes = ClassesManager.getSelected(gamePlayer);
            if (classes.equals(this) && e.getItem().hasItemMeta() && e.getItem().getItemMeta() != null && e.getItem().getItemMeta().getDisplayName() != null) {
                if (e.getItem().getItemMeta().getDisplayName().contains("速度药水")) {
                    e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 300, 4));
                }
                else if (e.getItem().getItemMeta().getDisplayName().contains("治疗药水") && e.getItem().getItemMeta().getDisplayName().contains("6")) {
                    PlayerUtils.heal(e.getPlayer(), 4.0);
                }
            }
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityDamageByEntity(final EntityDamageByEntityEvent e) {
        if (CitizensAPI.getNPCRegistry().isNPC(e.getEntity())) {
            return;
        }
        if (e.isCancelled()) {
            return;
        }
        if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
            final GamePlayer damager = GamePlayer.get(e.getDamager().getUniqueId());
            final GamePlayer player = GamePlayer.get(e.getEntity().getUniqueId());
            if (damager.isSpectator() || damager.getGameTeam().isInTeam(player)) {
                return;
            }
            final Classes classes = ClassesManager.getSelected(damager);
            if (classes.equals(this)) {
                if (Werewolf.skill.contains(damager)) {
                    PlayerUtils.heal(damager.getPlayer(), e.getDamage() * 0.05);
                }
                if (!Werewolf.skillDamage.contains(damager)) {
                    Werewolf.skillDamage.add(damager);
                    ((Player)e.getEntity()).damage(2.0);
                }
                this.secondSkill.use(damager, damager.getPlayerStats().getKitStats(classes));
            }
            else if (ClassesManager.getSelected(player).equals(this)) {
                player.addEnergy(2, PlayerEnergyChangeEvent.ChangeReason.MAGIC);
                Werewolf.skill2.put(player, 0);
            }
        }
        else if (e.getEntity() instanceof Player && e.getDamager() instanceof Arrow && ((Arrow)e.getDamager()).getShooter() instanceof Player) {
            final GamePlayer shooter = GamePlayer.get(((Player)((Arrow)e.getDamager()).getShooter()).getUniqueId());
            if (shooter.isSpectator() || shooter.getGameTeam().isInTeam(GamePlayer.get(e.getEntity().getUniqueId()))) {
                return;
            }
            final Classes classes2 = ClassesManager.getSelected(shooter);
            if (classes2.equals(this)) {
                this.secondSkill.use(shooter, shooter.getPlayerStats().getKitStats(classes2));
            }
        }
    }
    
    static {
        skill = new ArrayList<GamePlayer>();
        skillDamage = new ArrayList<GamePlayer>();
        skill2 = new HashMap<GamePlayer, Integer>();
    }
}
